extends VBoxContainer
# 武器升级弹框,被动升级也用的这个
# 指定武器槽链接,获取武器槽中的武器
@export var weapons : HBoxContainer
@export var passive_items : HBoxContainer
# 预加载升级页面
var OptionSolt = preload("res://Scenes/OptionSlot.tscn")

@export var particles : GPUParticles2D
@export var panel : NinePatchRect

# 节点创建时,先隐藏节点
func _ready() -> void:
	hide()
	particles.hide()
	panel.hide()

func get_available_resource_in(items) ->Array[Item]:
	var resources : Array[Item] = []
	for item in items.get_children():
		if item.item != null:
			resources.append(item.item)
	return resources

func add_option(item)->int:
	if item.is_upgradable():
		var option_solt = OptionSolt.instantiate()
		option_solt.item = item
		add_child(option_solt)
		return 1
	return 0
	

# 关闭升级弹窗时,开启场景树
func close_option()->void:
	hide()
	particles.hide()
	panel.hide()
	get_tree().paused = false

# 展示升级弹窗
func show_option()->void:
	var weapon_availble = get_available_resource_in(weapons)
	var passive_item_avaivle = get_available_resource_in(passive_items)
	if weapon_availble.size() == 0 || passive_item_avaivle.size() == 0:
		return
	# 如果有武器,则删除之前的选项
	for solt in get_children():
		solt.queue_free()
	var option_size :int = 0;
	for weapon in weapon_availble:
		option_size += add_option(weapon)
		
		if weapon.max_level_reached() and weapon.item_needed in passive_item_avaivle:
			var option_slot = OptionSolt.instantiate()
			option_slot.item = weapon
			add_child(option_slot)
			option_size += 1
			
	for passive_item in passive_item_avaivle:
		option_size += add_option(passive_item)
	if option_size == 0:
		return
	show()
	particles.show()
	panel.show()
	# 暂停游戏
	get_tree().paused = true
	
	
